package com.movitech.wechat.util;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Hashtable;
import javax.imageio.ImageIO;

public class Decoder
{
  public static String decode(String strUrl)
  {
    BufferedImage bufferedImage = null;
    try
    {
      URL url = new URL(strUrl);
      bufferedImage = ImageIO.read(url);
    }
    catch (IOException e) {
      e.printStackTrace();
    }

    LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);

    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    Hashtable hints = new Hashtable();

    hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

    Result result = null;
    try
    {
      result = new MultiFormatReader().decode(bitmap, hints);
    }
    catch (NotFoundException e)
    {
      return null;
    }

    return result.toString();
  }
}